package com.woniu.zsd.recycle.excel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.zsd.recycle.dto.RecycleDtatistics;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
public class ExcelExporter {

    private StringRedisTemplate stringRedisTemplate;
    private ObjectMapper objectMapper;

    // 构造函数注入依赖
    public ExcelExporter(StringRedisTemplate stringRedisTemplate, ObjectMapper objectMapper) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.objectMapper = objectMapper;
    }

    public void exportRecycleDataToExcel() {
        // 构造 Redis key （例如使用当前日期）
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        String key = formattedDate + "_RecycleSaleDay";

        // 从 Redis 获取数据
        String jsonString = stringRedisTemplate.opsForValue().get(key);
        if (jsonString == null) {
            System.out.println("Redis中没有数据!");
            return;
        }

        // 将 JSON 转换成 List<RecycleDtatistics>
        List<RecycleDtatistics> dataList;
        try {
            dataList = objectMapper.readValue(jsonString, new TypeReference<List<RecycleDtatistics>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        // 创建 Excel 工作簿和工作表
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("今日回收数据");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("废品类型");
        headerRow.createCell(1).setCellValue("总量");
        headerRow.createCell(2).setCellValue("总金额");

        // 填充数据
        int rowNum = 1;
        for (RecycleDtatistics dto : dataList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(dto.getMaterialType());
            // 如果需要对 BigDecimal 进行格式化，可以根据需求处理
            row.createCell(1).setCellValue(dto.getAllAmount() != null ? dto.getAllAmount().toString() : "");
            row.createCell(2).setCellValue(dto.getMoney() != null ? dto.getMoney().toString() : "");
        }

        // 可选：自动调整列宽
        for (int i = 0; i < 3; i++) {
            sheet.autoSizeColumn(i);
        }

        // 将工作簿写入文件
        try (FileOutputStream fos =new FileOutputStream("/opt/dockerzsd/reports/RecycleDataReport.xlsx")){
            workbook.write(fos);
            System.out.println("Excel报表生成成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}